import { postLogin } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken()
}
console.log(state.token)
const mutations = {
  getToken(state, token) {
    state.token = token
  }
}
const actions = {
  async login(context, data) {
    const res = await postLogin(data)
    const token = res.data.token
    context.commit('getToken', token)
    setToken(token)
  },
  logout(context) {
    removeToken()
    context.commit('getToken', '')
  }
}

export default {
  namespaced: true, // 开启命名空间
  state,
  mutations,
  actions
}

